﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>587</ArticlesID><Title>H&amp;#224;m getElementsByClassName</Title><Body>&amp;lt;p&amp;gt;Bản th&amp;amp;acirc;n ng&amp;amp;ocirc;n ngữ lập tr&amp;amp;igrave;nh JavaScript kh&amp;amp;ocirc;ng cung cấp cho ta h&amp;amp;agrave;m lấy một đối tượng th&amp;amp;ocirc;ng qua t&amp;amp;ecirc;n lớp, m&amp;amp;agrave; chỉ l&amp;amp;agrave; ID, t&amp;amp;ecirc;n v&amp;amp;agrave; thẻ. Nay với h&amp;amp;agrave;m n&amp;amp;agrave;y, ch&amp;amp;uacute;ng ta c&amp;amp;oacute; thể thực hiện điều n&amp;amp;agrave;y dễ d&amp;amp;agrave;ng.&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: jscript;&amp;quot;&amp;gt;
// Created by: Stuart Colville | http://muffinresearch.co.uk/
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || &amp;amp;quot;*&amp;amp;quot;;
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &amp;amp;amp;&amp;amp;amp;  strTag == &amp;amp;quot;*&amp;amp;quot; &amp;amp;amp;&amp;amp;amp;  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i &amp;amp;lt; j; i++) {
    var arrObjClass = objColl[i].classNasplit(' ');
    if (delim == ' ' &amp;amp;amp;&amp;amp;amp; arrClass.length &amp;amp;gt; arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k &amp;amp;lt; l; k++) {
      for (var m = 0, n = arrClass.length; m &amp;amp;lt; n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' &amp;amp;amp;&amp;amp;amp; c == 1) || (delim == ' ' &amp;amp;amp;&amp;amp;amp; c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;b&amp;gt;&amp;lt;i&amp;gt;Theo javascriptbank&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>11/5/2009 1:17:04 AM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/11/5/Ham-getElementsByClassName-24B</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>30</CategoryId><ArrayCatID>30|25</ArrayCatID></Article></Articles>